package com.gougou.context;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * 将容器中的上下文对象注入到上下文对象工具类中，以供后续使用
 *
 * @author EX-ZHANZHENQIANG001
 * @date 2018-04-19
 */
@Component
public class ApplicationContextUtils implements ApplicationContextAware {

    private ApplicationContext context;

    private static ApplicationContextUtils applicationContextUtils;

    public static ApplicationContextUtils getInstance() {
        return applicationContextUtils;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        //判断有没有当前对象
        if(null == applicationContextUtils) {
            applicationContextUtils = this;
        }
        this.setContext(applicationContext);
        System.out.println("abc");
    }

    public Object getBean(String beanName) {
        return getContext().getBean(beanName);
    }

    public ApplicationContext getContext() {
        return context;
    }

    public void setContext(ApplicationContext context) {
        this.context = context;
    }
}
